package stream;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author
 * @Title: UseSort
 * @Description:
 * @date 2022/12/14 1:29 下午
 */
public class UseSort {

    @Test
    public void test01(){
        Student s1 = new Student(22,"理想");
        Student s2 = new Student(12,"李四");
        Student s3 = new Student(8,"阿虎");

        List<Student> list = new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);

        Comparator<Student> comparing = Comparator.comparing(e->e.getName()); // function接口程序给你一个入参,要你返回一个出参
        list.sort(comparing); // 给list提供一个比较器 list本身被直接改变了
        System.out.println(list);

        //stream操作不会破坏list本身的顺序
        List<Student> sortList = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
        System.out.println(sortList);
    }

    @Test
    public void test02(){
        List<String> strList = new ArrayList<>();
        strList.add("zo");
        strList.add("ab");
        strList.add("lv");
        strList.sort(Comparator.naturalOrder());
        System.out.println(strList);
    }

}
